#' Compute GIRF
#'
#' @param delta the magnitude of IR
#' @param regime \code{up} or \code{dn}
#' @param impvar
#' @param impcnt
#'
GIRFTGVAR <- function(countryrlt,regdata, endovar,TradeWeight,regime = 'dn',
                      impvar = 'unctn',impcnt = 'USA', p =2, q = 1,
                      ncores = 6){
  cl <- parallel::makeCluster(ncores)
  doParallel::registerDoParallel(cl)
  ifelse (length(residuals(countryrlt[[impcnt]][['rawrlt']]$varresult$cpi)) == 0,
          nresi <- length(residuals(countryrlt[[impcnt]][['dn']]$varresult$cpi)),
          nresi <- length(residuals(countryrlt[[impcnt]][['rawrlt']]$varresult$cpi)))
  irf <- foreach (i = 1:nresi, .packages = c('magrittr','GVARX')) %dopar% {
    # print(i)
    devtools::load_all()
    ForeData1 <- list()
    for (j in 1:5){
      # up or down
      # 负向脉冲冲击
      ForeData1[[j]] <- GIRFTwoRegimeOnlyUpDn(regdata,endovar,countryrlt, nF = 24, p = p,
                            delta = -sd(regdata[regdata$ID %in% impcnt, impvar]), q=q,
                            impvar = impvar, impcnt = impcnt, k = i, regime = regime,
                            TradeWeight = TradeWeight)
    }
    ForeData1
  }
  parallel::stopCluster(cl)
  return(irf)
}

